package com.lhoverc.mapper;

import com.lhoverc.pojo.VO.CommentVO;
import com.lhoverc.pojo.VO.PostVO;
import com.lhoverc.pojo.entity.CommentEntity;
import com.lhoverc.pojo.entity.LikeEntity;
import com.lhoverc.pojo.entity.PostEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @Author: LiCheng
 * @CreateTime: 2025-05-03
 * @Description:
 * @Version: 1.0
 */
@Mapper
public interface PostMapper {
    /**
     * 获取post
     * @return
     */
    @Select("select * from post p, user u where p.is_delete = 0 and p.user_id = u.id order by p.release_time desc")
    List<PostVO> getPost();

    /**
     * 查询postId的所有评论
     * @param id
     * @return
     */
    @Select("select * from comment c, user u where c.is_delete = 0 and c.post_id = #{id} and c.user_id = u.id order by c.release_time desc")
    List<CommentVO> getComments(int id);

    /**
     * 添加评论
     * @param commentEntity
     */
    @Insert("insert into comment (user_id, post_id, content, release_time, create_time, update_time) VALUES " +
            "(#{userId}, #{postId}, #{content}, #{releaseTime}, #{createTime}, #{updateTime})")
    void addComment(CommentEntity commentEntity);

    /**
     * 添加帖子
     * @param postEntity
     */
    @Insert("insert into post (user_id, content, release_time, create_time, update_time) VALUES " +
            "(#{userId}, #{content}, #{releaseTime}, #{createTime}, #{updateTime})")
    void addPost(PostEntity postEntity);

    /**
     * 取消点赞
     * @param likeEntity
     */
    @Update("update likes set is_delete = 1, update_time = #{updateTime} where user_id = #{userId} and post_id = #{postId}")
    void deleteLike(LikeEntity likeEntity);

    /**
     * 减少点赞数量
     * @param likeEntity
     */
    @Update("update post set likes = likes-1, update_time = #{updateTime} where id = #{postId}")
    void reducePostLikes(LikeEntity likeEntity);

    /**
     * 通过用户id和帖子id查询点赞id
     * @param likeEntity
     * @return
     */
    @Select("select id from likes where post_id = #{postId} and user_id = #{userId}")
    Integer selectLike(LikeEntity likeEntity);

    /**
     * 添加一个点赞信息
     * @param likeEntity
     */
    @Insert("insert into likes (user_id, post_id, create_time, update_time) VALUES " +
            "(#{userId}, #{postId}, #{createTime}, #{updateTime})")
    void addLike(LikeEntity likeEntity);

    /**
     * 为post表的like属性加1
     * @param likeEntity
     */
    @Update("update post set likes = likes+1 where id = #{postId}")
    void addPostLikes(LikeEntity likeEntity);

    /**
     * 修改like数据的is_delete属性，同时更新update_time
     * @param likeEntity
     */
    @Update("update likes set is_delete = 0, update_time = #{updateTime} where user_id = #{userId} and post_id = #{postId}")
    void updateIsDelete(LikeEntity likeEntity);

    /**
     * 通过user_id查询所有的post_id
     * @param likeEntity
     * @return
     */
    @Select("select post_id from likes where is_delete = 0 and user_id = #{userId}")
    List<Integer> getLikes(LikeEntity likeEntity);

}
